﻿using B.MESUI.Login.A_Public.Helpers;
using B.MESUI.Login.BasicData_UI.BasicData;
using B.MESUI.Login.Properties;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using Standard;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace B.MESUI.Login.BasicData_UI.Customer
{
    public partial class EditCustomerForm : DevExpress.XtraEditors.XtraForm
    {

        public delegate void EditCustomerEvent();
        public event EditCustomerEvent Customer_Event;
        public UpdateCustomer CustomerDTO;
        public EditCustomerForm(UpdateCustomer customerDTO)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            
            CustomerDTO = customerDTO;
            LoadCustomerData();
        }
        /// <summary>
        /// 加载供应商数据到表单
        /// </summary>
        private void LoadCustomerData()
        {
            if (CustomerDTO == null)
                return;
            CtextEdit1.Text = CustomerDTO.ID;
            CtextEdit2.Text = CustomerDTO.customeR_NAME;
            CtextEdit3.Text = CustomerDTO.customeR_PHONE;
            CtextEdit4.Text = CustomerDTO.crediT_CODE;
            CtextEdit5.Text = CustomerDTO.customeR_CODE;
            CtextEdit6.Text = CustomerDTO.customeR_ABBREVIATION;
            CtextEdit7.Text = CustomerDTO.customeR_ADDRESS;
            CtextEdit8.Text = CustomerDTO.customeR_NODE;

        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateCustomer customerDTO = new UpdateCustomer();
                // 收集表单数据
                customerDTO.ID = CtextEdit1.Text.Trim();
                customerDTO.customeR_NAME = CtextEdit2.Text.Trim();
                customerDTO.customeR_PHONE = CtextEdit3.Text.Trim();
                customerDTO.crediT_CODE = CtextEdit4.Text.Trim();
                customerDTO.customeR_CODE = CtextEdit5.Text.Trim();
                customerDTO.customeR_ABBREVIATION = CtextEdit6.Text.Trim();
                customerDTO.customeR_ADDRESS = CtextEdit7.Text.Trim();
                customerDTO.customeR_NODE = CtextEdit8.Text.Trim();


                // 调用API
                string url = $"http://localhost:5198/api/Customer/UpdateCustomer";
                string result = await HttpClientHelper.PutAsync(url, customerDTO);

                // 解析API返回结果
                var apiResult = JsonConvert.DeserializeObject<ApiResult<object>>(result);

                if (apiResult != null && apiResult.Code == 200)
                {
                    XtraMessageBox.Show("供应商信息修改成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    // 触发事件通知父窗体刷新数据
                    Customer_Event?.Invoke();

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    XtraMessageBox.Show($"修改失败：{apiResult?.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show($"发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

    }
}